window.addEvent('domready', function() {
	var fieldset = $$('fieldset.login')[0];
	var form = fieldset.getChildren('form');
	
	var username = $('username');
	var password = $('password');
	var mail 	 = $('mail');
	var remember = $('remember');

	// uzupelnianie danych
	if(Cookie.read('remember') == 'true') {
		var usernameValue = Cookie.read('username');
		var passwordValue = Cookie.read('password');
		var mailValue 	  = Cookie.read('mail');
		
		username.setProperty('value', usernameValue);
		password.setProperty('value', passwordValue);
		mail.setProperty('value', mailValue);
		remember.setProperty('checked', true);
	} else {
		remember.setProperty('checked', false);
	}

	form.addEvent('submit',function(e){
		var usernameValue = username.getProperty('value');
		var passwordValue = password.getProperty('value');
		var mailValue 	  = mail.getProperty('value');

		// zapis danych
		if(remember.getProperty('checked')) {
			Cookie.write('username', usernameValue, {'duration':30});
			Cookie.write('password', passwordValue, {'duration':30});
			Cookie.write('mail', mailValue, {'duration':30});
			Cookie.write('remember', true, {'duration':30});
		} else {
			Cookie.write('remember', false, {'duration':30});
		}

		var uri,usernameField,passwordField = null;

		switch(mailValue) {
			case 'horde':
				uri = 'http://webmail.promotor.krakow.pl/horde/imp/redirect.php';
				usernameField = 'imapuser';
				passwordField = 'pass';
				break;
			case 'squirrel':
				uri = 'http://webmail2.promotor.krakow.pl/src/redirect.php';
				usernameField = 'login_username';
				passwordField = 'secretkey';
				form.adopt(new Element('input',{'type':'hidden','name':'load_frameset','value':1}));
				break;
			default:
				// przerwij lancuch i nie przetwarzaj dalej
				return;
		}

		form.setProperty('action',uri);
		username.setProperty('name', usernameField);
		password.setProperty('name', passwordField);
	});
});